
//  WFChannelDetailViewController.m
//  FAKER
//
//  Created by Jianwei on 15/11/7.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "WFChannelDetailViewController.h"
#import "AllHeader.h"
#import "FitHeader.h"
#import "BaseClassCollectionView.h"
#import "WFChannelDetailCollectionViewCell.h"
#import "WFChannelDetailModel.h"
#import "WFModelSingleton.h"
#import "WFChannelWebViewController.h"
#define WFUrlString @"http://weibo.com/u/5290447258"
@interface WFChannelDetailViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, WFChannelDetailCollectionViewCellDelegate, UIViewControllerPreviewingDelegate>
@property(nonatomic, strong)BaseClassCollectionView *backCollectionView;
@property(nonatomic, strong)NSMutableArray *dataSourceArray;
@property(nonatomic, assign)NSInteger index;
@property(nonatomic, strong)YHLoadingView *loadingView;
@property(nonatomic, strong)YHBottomView *bottomView;
@end

@implementation WFChannelDetailViewController
- (void)pushWFchannelWebViewControllerWithModel:(WFChannelDetailModel *)model
{
    WFChannelWebViewController *wc = [[WFChannelWebViewController alloc]init];
    wc.model = model;
    [self.navigationController pushViewController:wc animated:1];
}
- (void)viewWillAppear:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:0 animated:YES];
    self.navigationController.navigationBar.hidden = 1;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
//    [self.navigationController setToolbarHidden:YES animated:NO];
}
- (void)getData
{
    [InternetDataRequest getDataByString:_api_url BodyDic:nil WithDataBlock:^(id data) {
        [_loadingView removeFromSuperview];
        
        WFChannelDetailInfoModel *model = [[WFChannelDetailInfoModel alloc]init];
        [model setValuesForKeysWithDictionary:[data objectForKey:@"data"]];
        [WFModelSingleton shareInstance].model = model;
        
        NSMutableArray *array = [[data objectForKey:@"data"] objectForKey:@"articles"];
        NSMutableArray *arr = [NSMutableArray arrayWithArray:array];
        if(array.count % 6 != 0)
        {
            NSInteger l = 36 - array.count;
            for (NSInteger i = 0; i < l; i ++) {
//                NSDictionary *dic = @{@"auther_name":@"李明", @"title":@"谁是蓝鸥最帅的男老师?"};
                NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjects:@[@"最帅男讲师_李明", @"我生平遇到过最帅的人"] forKeys:@[@"auther_name", @"title"]];
                [arr insertObject:dic atIndex:arc4random() % (arr.count + 1 + i)];
//                [array addObject:dic];
            }
        }
        
        for (NSInteger i = 0; i < 6; i ++) {
            NSMutableArray *dataArray = [NSMutableArray array];
            for (NSInteger j = 0; j < 6; j ++) {
                WFChannelDetailModel *model = [[WFChannelDetailModel alloc]init];
                [model setValuesForKeysWithDictionary:arr[i * 6 + j]];
//                NSLog(@"%@", model.weburl);
                [dataArray addObject:model];
            }
            for (NSInteger k = 0; k < 6; k ++) {
                if ([[dataArray[k] media] count] != 0) {
                    [dataArray exchangeObjectAtIndex:k withObjectAtIndex:0];
//                    NSLog(@"%@", [[[dataArray[0] media] objectAtIndex:0] objectForKey:@"url"]);
                    break;
                }
                
            }
                    [_dataSourceArray addObject:dataArray];
        }
//        [self createHeaderView];
        [_backCollectionView reloadData];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //创建加载中视图
    _loadingView = [[YHLoadingView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 40)];
    [self.view addSubview:_loadingView];
}
- (void)loadView
{
    [super loadView];
//    NSLog(@"%@", _api_url);
    _dataSourceArray = [NSMutableArray array];
    
    _index = 0;
    [self getData];
    [self registerForPreviewingWithDelegate:self sourceView:self.view];
    
//    [self.navigationController setToolbarHidden:NO animated:NO];
//
//    if (NSUSERDEFAULTS_NIGHT) {
//        [self.navigationController.toolbar setBarTintColor:BGCOLOR_BLACK];
//    }
//    else
//    {
//        [self.navigationController.toolbar setBarTintColor:BGCOLOR_WHITE];
//    }
    
    
    UICollectionViewFlowLayout *backCollectionViewFlowLayout = [[UICollectionViewFlowLayout alloc]init];
    backCollectionViewFlowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 100);
    backCollectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(80, 0, 0, 0);
    backCollectionViewFlowLayout.minimumInteritemSpacing = 0;
    backCollectionViewFlowLayout.minimumLineSpacing = 0;
    backCollectionViewFlowLayout.scrollDirection = 1;
    
    
    _backCollectionView = [[BaseClassCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 40) collectionViewLayout:backCollectionViewFlowLayout];
    _backCollectionView.pagingEnabled = 1;
    _backCollectionView.dataSource = self;
    _backCollectionView.delegate = self;
    [_backCollectionView registerClass:[WFChannelDetailCollectionViewCell class] forCellWithReuseIdentifier:@"back"];
    [self.view addSubview:_backCollectionView];
    
    
    
    
    //创建底部条
    _bottomView = [[YHBottomView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 40, SCREEN_WIDTH, 40)];
    void (^bottomButtonBlock)(NSString *) = ^(NSString *buttonTitle) {
        if ([buttonTitle isEqualToString:@"back"]) {
            [self.navigationController popViewControllerAnimated:YES];
        }
    };
    _bottomView.block = bottomButtonBlock;
    [self.view addSubview:_bottomView];
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    _index = indexPath.row;
    WFChannelDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"back" forIndexPath:indexPath];
    if (_dataSourceArray.count != 0) {
        cell.dataArray = _dataSourceArray[indexPath.row];
        
//        cell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
//        [UIView animateWithDuration:0.5 animations:^{
//            cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
//        }];
    }
    cell.delegate = self;
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
    WFChannelWebViewController *wc = [[WFChannelWebViewController alloc]init];
    wc.model = [_dataSourceArray[_index] objectAtIndex:[self getIndexFromLocation:location]];
    wc.preferredContentSize = CGSizeMake(0, 500);
    //    CGRect rect = CGRectMake(10, location.y - 10, SCREEN_WIDTH - 20, 20);
    //    previewingContext.sourceRect = rect;
    return wc;
}

- (NSInteger)getIndexFromLocation:(CGPoint)location
{
    //图片高210
    //没有图片 四等分
    if ([[_dataSourceArray[_index] objectAtIndex:0] media].count) {
        if (location.y < 330) {
            return 0;
        }
        else if(location.y > 667 - 337 / 3)
        {
            return 5;
        }
//        else if(location.y > 330 + 337 / 3 && location.y + 2 * 337 / 3)
        else if(location.y > (330 + 337 / 3))
        {
            if (location.x < 375 / 2) {
                return 3;
            }
            else
            {
                return 4;
            }
        }
        else
        {
            if (location.x < 375 / 2) {
                return 1;
            }
            else
            {
                return 2;
            }
        }
    }
    else
    {
        if (location.y < 120 + 547 / 4) {
            return 0;
        }
        else if(location.y > 120 + 547 / 4 * 3)
        {
            return 5;
        }
        else if(location.y > 120 + 547 / 2)
        {
            if (location.x < 375 / 2) {
                return 3;
            }
            else
            {
                return 4;
            }
        }
        else
        {
            if (location.x < 375 / 2) {
                return 1;
            }
            else
            {
                return 2;
            }
        }
    }

}


- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
    [self showViewController:viewControllerToCommit sender:self];
    //[self collectionView:_backCollectionView didSelectItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}

//- (void)createHeaderView
//{
//    BaseClassImageView *imageView = [[BaseClassImageView  alloc]initWithFrame:CGRectMake(127.5, -20, 120, 120)];
//    imageView.contentMode = 0;
//    imageView.clipsToBounds = 0;
//    [_backCollectionView addSubview:imageView];
//    [imageView sd_setImageWithURL:[NSURL URLWithString:[[WFModelSingleton shareInstance].model.block_info objectForKey:@"pic"]] placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//        imageView.image = [image imageWithColor:[UIColor RGBFromHexColor:[[WFModelSingleton shareInstance].model.block_info objectForKey:@"block_color"]]];
//    }];
//    for (NSInteger i = 0; i < 2; i++) {
//        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(20 + 205 * i, 63, 130, 1)];
//        view.backgroundColor = [UIColor blackColor];
//        [self.view addSubview:view];
//    }
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
